# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from collections import defaultdict
from datetime import datetime, date, time
from odoo.exceptions import ValidationError
import pytz

from odoo import fields, models, _, api
from odoo.exceptions import UserError

class ModifiableWorkProductionLine(models.Model):
    _name = 'modifiable.work.production.line'
    _description = 'Modifiable Manufacturing Order Line'

    change_id = fields.Many2one('sb.order.internal.change', string='Order Internal Change')
    production_id = fields.Many2one('mrp.production', string='Manufacturing Order')
    customer = fields.Many2one('res.partner', related='production_id.customer', string='Customer')
    product_qty = fields.Float(related='production_id.product_qty', string='Quantity To Produce')
    panel_qty = fields.Float(related='production_id.panel_qty', string='PANEL quantity')
    lot_producing_id = fields.Many2one('stock.lot', related='production_id.lot_producing_id',
                                       string='Lot/Serial Number')
    date_strat = fields.Datetime(related='production_id.date_start', string='Start')
    state = fields.Selection([
        ('draft', 'Draft'),
        ('confirmed', 'Confirmed'),
        ('progress', 'In Progress'),
        ('to_close', 'To Close'),
        ('done', 'Done'),
        ('cancel', 'Cancelled')], string='State', related='production_id.state')
    changeable = fields.Boolean(string='Changeable')
    to_change = fields.Boolean(string='To Change')
    unchangeable_reason = fields.Char(string='Unchangeable Reason')
    reason_details = fields.One2many('unchangeable.reason.detail', 'line_id', string='Reason Details')
    change_state = fields.Selection([
        ('unchangeable', 'Unchangeable'),
        ('unchange', 'Unchange'),
        ('tochange', 'To Change'),
        ('changed', 'Changed'),
    ], string='Change State', compute='_compute_change_state', store=True)

    @api.depends('changeable', 'to_change', 'change_id.state')
    def _compute_change_state(self):
        for rec in self:
            if not rec.changeable:
                rec.change_state = 'unchangeable'
            elif not rec.to_change:
                rec.change_state = 'unchange'
            elif rec.change_id.state != 'done':
                rec.change_state = 'tochange'
            else:
                rec.change_state = 'changed'

    def act_unchangeable_reason_details(self):
        return {
            'name': _('Unchangeable Reason Details'),
            'view_mode': 'form',
            'view_type': 'form',
            'res_model': 'modifiable.work.production.line',
            'target': 'new',
            'type': 'ir.actions.act_window',
            'res_id': self.id
        }

class ModifiableWorkProductionLine(models.Model):
    _name = 'unchangeable.reason.detail'
    _description = 'MO Unchangeable Reason Detail'

    line_id = fields.Many2one('modifiable.work.production.line', string='related line')
    mrp_production_id = fields.Many2one('mrp.production', string='Manufacturing Order')
    type = fields.Selection([
        ('operation', 'bom operation'),
        ('move', 'stock move'),
        ('bom', 'bom qty'),
    ], string='Type')
    detail = fields.Char(string='Reason')